package controllers;

import play.*;
import play.db.jpa.JPABase;
import play.libs.Mail;
import play.mvc.*;

import groovy.ui.Console;

import java.lang.reflect.Type;
import java.util.*;

import javax.mail.Session;
import javax.naming.directory.SearchResult;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.eclipse.jdt.internal.core.index.Index;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

import models.*;

@With(Secure.class)
public class Application extends Controller {
	public static String Area;
	public static Date Date;

	public static  void add_area() {
		render();
	}
	
	public static  void get_all_area() {
		ArrayList areaList = (ArrayList)Area_loc.findAll();
		render(areaList);
	}
	
	public static ArrayList<String> areaDate = new ArrayList<String>();

	public static void income_chooseDate() {
		render();

	}

	public static void createOperationReport() {

		ArrayList<Product> products = calculate_renderProducts(Area, Date);
		ArrayList<Integer> dirty = getDirty(Date, Area);
		ArrayList<Integer> clean = getClean(Date, Area);
		ArrayList<Integer> New = getNew(Date, Area);

		String[] salesList = params.getAll("sales");
		String[] lostList = params.getAll("lost");
		String[] totalOutList = params.getAll("totalOut");
		String[] totalInList = params.getAll("totalIn");

		for (int i = 0; i < salesList.length; i++) {
			if (totalInList[i] != "" && totalOutList[i] != ""
					&& salesList[i] != "" && lostList[i] != "") {
				int totalin = Integer.parseInt(totalInList[i]);
				int totalout = Integer.parseInt(totalOutList[i]);
				int sales = Integer.parseInt(salesList[i]);
				int lost = Integer.parseInt(lostList[i]);
				int x = i;
				if (totalin != totalout - sales - lost) {
					flash.error("Not Equal at line " + ++x);
					operations(Date, Area);

				}

			}
		}

		OperationReport or = new OperationReport(Area, Date, dirty, clean, New,
				salesList, lostList, totalInList, totalOutList);
		or.save();
		render(or);
	}

	public static void postScreen_chooseDate() {
		render();
	}

	public static void postScreen(String area, Date date) {
		render();
	}

	public static void income(String area, Date date) {
		Area = area;
		Date = date;

		int month = Date.getMonth() + 1;

		int year = Date.getYear() + 1900;
		String dateString = Date.getDate() + "/" + month + "/" + year;
		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<Contract> renderContracts = new ArrayList<Contract>();
		ArrayList<String> renderCash = new ArrayList<String>();
		int numberOfChangePerMonth = 0;

		for (int i = 0; i < allContracts.size(); i++) {
			for (int j = 0; j < allContracts.get(i).reportList.size(); j++) {
				if (allContracts.get(i).area.equalsIgnoreCase(Area)) {
					if (!allContracts.get(i).reportList.get(j).cr.isEmpty()
							&& compareDateDate(
									allContracts.get(i).reportList.get(j).date,
									Date)) {
						renderContracts.add(allContracts.get(i));
						renderCash
								.add(allContracts.get(i).reportList.get(j).cr);
						numberOfChangePerMonth = allContracts.get(i).productList
								.get(0).number_of_changes;
					}
				}
			}
		}
		// System.out.println("cash number are "+renderCash.toString());

		render(renderContracts, dateString, renderCash, numberOfChangePerMonth);
	}

	public static void index() {
		render();
	}

	public static void search() {
		render();
	}

	public static void create() {

		render();
	}

	public static void chooseDate() {
		render();
	}

	public static ArrayList<Contract> calculate_returnedContracts(Date date,
			String area) {
		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<Contract> returnContracts = new ArrayList<Contract>();
		ArrayList<Long> temp = new ArrayList<Long>();
		for (int i = 0; i < allContracts.size(); i++) {
			for (int j = 0; j < allContracts.get(i).productList.size(); j++) {

				if (compareDate(
						allContracts.get(i).productList.get(j).change_time_list,
						date)) {
					if (!temp.contains(allContracts.get(i).id)) {

						temp.add(allContracts.get(i).id);
						returnContracts.add(allContracts.get(i));
					}
				}
			}

		}
		return returnContracts;

	}

	public static boolean compareDate(LinkedList<Date> list, Date date2) {

		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getDate() == date2.getDate()
					&& list.get(i).getMonth() == date2.getMonth()
					&& list.get(i).getYear() == date2.getYear())
				return true;
		}

		return false;
	}

	public static void showDate(Date date, String area) {
		List<Contract> allContracts = Contract.findAll();
		ArrayList<Contract> returnContracts = new ArrayList<Contract>();
		ArrayList<Product> returnProducts = new ArrayList<Product>();
		ArrayList<Long> temp = new ArrayList<Long>();

		// Contract c = (Contract) Contract.findAll().get(0);

		for (int i = 0; i < allContracts.size(); i++) {

			for (int j = 0; j < allContracts.get(i).productList.size(); j++) {

				if (compareDate(
						allContracts.get(i).productList.get(j).change_time_list,
						date)
						&& allContracts.get(i).area.equalsIgnoreCase(area)) {

					if (!temp.contains(allContracts.get(i).id)) {
						temp.add(allContracts.get(i).id);
						returnContracts.add(allContracts.get(i));
					}
					returnProducts.add(allContracts.get(i).productList.get(j));

				}
			}
		}
		render(returnContracts, returnProducts, area, date);

	}

	public static void showDate2(Date date, String area) {
		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<Product> returnProducts = new ArrayList<Product>();
		ArrayList<String> temp = new ArrayList<String>();

		int[] no_of_products = new int[5];
		for (int i = 0; i < allContracts.size(); i++) {

			for (int j = 0; j < allContracts.get(i).productList.size(); j++) {

				if (allContracts.get(i).productList.get(j).change_time_list
						.contains(date)) {
					// System.out.println("flag is "+returnProducts.contains("Mop"));
					if (!temp
							.contains(allContracts.get(i).productList.get(j).name)) {
						// temp.add(allContracts.get(i).productList.get(j).name);
						returnProducts.add(allContracts.get(i).productList
								.get(j));
						if (allContracts.get(i).productList.get(j).name
								.equalsIgnoreCase("Mop18")) {
							no_of_products[0] = no_of_products[0]
									+ allContracts.get(i).productList.get(j).quantity;
						}
						if (allContracts.get(i).productList.get(j).name
								.equalsIgnoreCase("Mit")) {
							no_of_products[1] = no_of_products[1]
									+ allContracts.get(i).productList.get(j).quantity;
						}
						if (allContracts.get(i).productList.get(j).name
								.equalsIgnoreCase("HD1")) {
							no_of_products[2] = no_of_products[2]
									+ allContracts.get(i).productList.get(j).quantity;
						}
						if (allContracts.get(i).productList.get(j).name
								.equalsIgnoreCase("HD2")) {
							no_of_products[3] = no_of_products[3]
									+ allContracts.get(i).productList.get(j).quantity;
						}
						if (allContracts.get(i).productList.get(j).name
								.equalsIgnoreCase("Mop36")) {
							no_of_products[4] = no_of_products[4]
									+ allContracts.get(i).productList.get(j).quantity;
						}
					}

				}
			}
		}

		ArrayList<myNumber> n = new ArrayList<myNumber>();
		if (no_of_products[0] != 0)
			n.add(new myNumber(no_of_products[0]));

		if (no_of_products[1] != 0)
			n.add(new myNumber(no_of_products[1]));

		if (no_of_products[2] != 0)
			n.add(new myNumber(no_of_products[2]));

		if (no_of_products[3] != 0)
			n.add(new myNumber(no_of_products[3]));

		if (no_of_products[4] != 0)
			n.add(new myNumber(no_of_products[4]));

		render(returnProducts, n);
	}

	public static void searchByPhone(String mobile) {
		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<Contract> returnContracts = new ArrayList<Contract>();

		for (int i = 0; i < allContracts.size(); i++) {
			if (allContracts.get(i).mobile1.equals(mobile)
					|| allContracts.get(i).mobile2.equals(mobile)
					|| allContracts.get(i).phone1.equals(mobile)
					|| allContracts.get(i).phone2.equals(mobile)) {
				returnContracts.add(allContracts.get(i));
			}
		}
		render(returnContracts);
	}

	public static void searchByArea(String area) {
		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<Contract> returnContracts = new ArrayList<Contract>();

		for (int i = 0; i < allContracts.size(); i++) {
			if (allContracts.get(i).area.equalsIgnoreCase(area)) {
				returnContracts.add(allContracts.get(i));
			}
		}
		render(returnContracts);
	}

	public static void carReturn_chooseDate() {

		render();

	}

	public static ArrayList<Product> calculate_renderProducts(String area,
			Date date) {
		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<Contract> returnContracts = new ArrayList<Contract>();
		ArrayList<Product> returnProducts = new ArrayList<Product>();
		ArrayList<Long> temp = new ArrayList<Long>();

		for (int i = 0; i < allContracts.size(); i++) {

			for (int j = 0; j < allContracts.get(i).productList.size(); j++) {

				if (compareDate(
						allContracts.get(i).productList.get(j).change_time_list,
						date)) {
					if (!temp.contains(allContracts.get(i).id)) {
						temp.add(allContracts.get(i).id);
						returnContracts.add(allContracts.get(i));
					}
					returnProducts.add(allContracts.get(i).productList.get(j));

				}
			}
		}
		return returnProducts;
	}

	public static int createCarReturnReport(String area, Date date) {
		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<Date> dates = new ArrayList<Date>();
		CarReturnReport report = null;
		for (int i = 0; i < allContracts.size(); i++) {

			if (allContracts.get(i).reportList.size() == 0) {
				for (int j = 0; j < allContracts.get(i).productList.size(); j++) {
					for (int k = 0; k < allContracts.get(i).productList.get(j).change_time_list.size(); k++) {
						if (!dates.contains(allContracts.get(i).productList.get(j).change_time_list.get(k))) {
						//	dates.add(allContracts.get(i).productList.get(j).change_time_list.get(k));
						//	System.out.println(i);
							report = new CarReturnReport(
									allContracts.get(i),
									allContracts.get(i).area,
									allContracts.get(i).productList.get(j).change_time_list
											.get(k), new ArrayList<Product>(),
									new ArrayList<Product>(),
									new ArrayList<Product>(),
									new ArrayList<Product>(), "");
							report.contract = allContracts.get(i);
							report.save();

							if (!allContracts.get(i).reportList
									.contains(report)) {
								allContracts.get(i).reportList.add(report);
							}

						}
					}

				}
			}
		}

		return CarReturnReport.findAll().size();

	}

	public static void comeBack(String area, Date date) {

		Area = area;
		Date = date;
		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<CarReturnReport> renderReportList = new ArrayList<CarReturnReport>();
		int size = createCarReturnReport(area, date);

		for (int i = 0; i < allContracts.size(); i++) {
			//System.out.println("baraaaaaaaaaaaaaaaaa "+ allContracts.get(1).reportList.size());
			for (int j = 0; j < allContracts.get(i).reportList.size(); j++) {
			//	System.out.println("gowa" + i);
				if (allContracts.get(i).reportList.get(j).area.equals(Area)
						&& compareDateDate(
								allContracts.get(i).reportList.get(j).date,
								Date)) {

					// System.out.println("ely gowaaaaaaaaaaaaaaaaaa " + i);
					renderReportList.add(allContracts.get(i).reportList.get(j));

				}
			}

		}
		render(renderReportList);

	}

	private static boolean compareDateDate(java.util.Date date1,
			java.util.Date date2) {
		if (date1.getDate() == date2.getDate()
				&& date1.getMonth() == date2.getMonth()
				&& date1.getYear() == date2.getYear())
			return true;
		return false;
	}

	public static boolean isNumeric(String str) {
		try {
			double d = Double.parseDouble(str);
		} catch (NumberFormatException nfe) {
			return false;
		}
		return true;
	}

	public static void findReport(Contract c) {

	}

	public static void clearReport(Long id) {
		CarReturnReport report = CarReturnReport.findById(id);

		report.cancelList.clear();
		report.newList.clear();
		report.dirtyList.clear();
		report.cleanList.clear();
		report.save();

		comeBack(Area, Date);

	}

	public static void carReturn(Long id, String cash_receipt, boolean NEW,
			boolean cancel) {
		Contract c = Contract.findById(id);
		for (int i = 0; i < c.reportList.size(); i++) {
			if (c.reportList.get(i).area.equalsIgnoreCase(Area)
					&& compareDateDate(c.reportList.get(i).date, Date)) {

				if (cash_receipt.equalsIgnoreCase("B") && !NEW && !cancel) {

					for (int j = 0; j < c.productList.size(); j++) {
						if (!c.reportList.get(i).cleanList
								.contains(c.productList.get(j))) {
							c.reportList.get(i).cleanList.add(c.productList
									.get(j));
							c.reportList.get(i).save();
							c.save();
						}
					}
				} else if (isNumeric(cash_receipt) && !NEW && !cancel) {

					for (int j = 0; j < c.productList.size(); j++) {
						if (c.reportList.get(i).dirtyList != null)
							if (!c.reportList.get(i).dirtyList
									.contains(c.productList.get(j))) {

								c.reportList.get(i).dirtyList.add(c.productList
										.get(j));
								// ssssssssss

								c.reportList.get(i).cr = cash_receipt;
								c.reportList.get(i).save();
								c.save();
							}
					}

				} else if (isNumeric(cash_receipt) && NEW && !cancel) {

					for (int j = 0; j < c.productList.size(); j++) {
						if (!c.reportList.get(i).newList.contains(c.productList
								.get(j))) {
							c.reportList.get(i).newList.add(c.productList
									.get(j));
							c.reportList.get(i).cr = cash_receipt;
							c.reportList.get(i).save();
							c.save();
						}
					}

				} else if (cancel && cash_receipt.isEmpty() && !NEW) {

					for (int j = 0; j < c.productList.size(); j++) {
						if (!c.reportList.get(i).cancelList
								.contains(c.productList.get(j))
								&& !c.reportList.get(i).dirtyList
										.contains(c.productList.get(j))) {
							c.reportList.get(i).cancelList.add(c.productList
									.get(j));
							c.reportList.get(i).dirtyList.add(c.productList
									.get(j));
							c.reportList.get(i).save();
							c.save();

						}
					}

				}

			}

		}

		comeBack(Area, Date);

	}

	public static void carContent_chooseDate() {
		render();

	}

	public static void operations_chooseDate() {

		render();

	}

	public static ArrayList<Integer> getDirty(Date date, String area) {
		ArrayList<Integer> dirty = new ArrayList<Integer>();

		ArrayList<Contract> returnedContracts = calculate_returnedContracts(
				date, area);

		for (int i = 0; i < returnedContracts.size(); i++) {

			for (int j = 0; j < returnedContracts.get(i).reportList.size(); j++) {

				if (returnedContracts.get(i).reportList.get(j).area
						.equalsIgnoreCase(area)
						&& compareDateDate(
								returnedContracts.get(i).reportList.get(j).date,
								date)) {

					for (int k = 0; k < returnedContracts.get(i).reportList
							.get(j).dirtyList.size(); k++) {
						dirty.add(returnedContracts.get(i).reportList.get(j).dirtyList
								.get(k).quantity);
					}
				}

			}
		}

		return dirty;

	}

	public static ArrayList<Integer> getClean(Date date, String area) {
		ArrayList<Integer> clean = new ArrayList<Integer>();

		ArrayList<Contract> returnedContracts = calculate_returnedContracts(
				date, area);
		for (int i = 0; i < returnedContracts.size(); i++) {

			for (int j = 0; j < returnedContracts.get(i).reportList.size(); j++) {

				if (returnedContracts.get(i).reportList.get(j).area
						.equalsIgnoreCase(area)
						&& compareDateDate(
								returnedContracts.get(i).reportList.get(j).date,
								date)) {
					for (int k = 0; k < returnedContracts.get(i).reportList
							.get(j).cleanList.size(); k++) {
						clean.add(returnedContracts.get(i).reportList.get(j).cleanList
								.get(k).quantity);
					}
				}

			}

		}

		return clean;

	}

	public static ArrayList<Integer> getNew(Date date, String area) {
		ArrayList<Integer> New = new ArrayList<Integer>();

		ArrayList<Contract> returnedContracts = calculate_returnedContracts(
				date, area);
		for (int i = 0; i < returnedContracts.size(); i++) {

			for (int j = 0; j < returnedContracts.get(i).reportList.size(); j++) {

				if (returnedContracts.get(i).reportList.get(j).area
						.equalsIgnoreCase(area)
						&& compareDateDate(
								returnedContracts.get(i).reportList.get(j).date,
								date)) {
					for (int k = 0; k < returnedContracts.get(i).reportList
							.get(j).newList.size(); k++) {
						New.add(returnedContracts.get(i).reportList.get(j).newList
								.get(k).quantity);
					}
				}

			}

		}

		return New;

	}

	public static void operations(Date date, String area) {
		Area = area;
		Date = date;
		ArrayList<Product> renderProducts = calculate_renderProducts(area, date);
		// ArrayList<Contract> returnedContracts =
		// calculate_returnedContracts(date, area);
		OperationReport or = new OperationReport();
		if (OperationReport.find("byAreaAndDate", Area, date).fetch().size() > 0) {
			or = (OperationReport) OperationReport
					.find("byAreaAndDate", Area, date).fetch().get(0);
			System.out.println(or.dirty + "\n" + or.sales);

		}

		ArrayList<Integer> dirty = new ArrayList<Integer>();
		ArrayList<Integer> clean = new ArrayList<Integer>();
		ArrayList<Integer> new_products = new ArrayList<Integer>();
		dirty = getDirty(date, area);
		clean = getClean(date, area);
		new_products = getNew(date, area);
		System.out.println("dirty is " + or);

		render(renderProducts, dirty, clean, new_products, or);
	}

	public static void carContent(Date date, String area) {

		int mop18 = 0;
		int mop18Jasmine = 0;
		int mop18Straw = 0;
		int mop185o5 = 0;
		int mop18Lemon = 0;

		int mit = 0;
		int mitJasmine = 0;
		int mitStraw = 0;
		int mit5o5 = 0;
		int mitLemon = 0;

		int hd1 = 0;
		int hd1Jasmine = 0;
		int hd1Straw = 0;
		int hd15o5 = 0;
		int hd1Lemon = 0;

		int hd2 = 0;
		int hd2Jasmine = 0;
		int hd2Straw = 0;
		int hd25o5 = 0;
		int hd2Lemon = 0;

		int mop36 = 0;
		int mop36Jasmine = 0;
		int mop36Straw = 0;
		int mop365o5 = 0;
		int mop36Lemon = 0;

		ArrayList<Product> renderProducts = calculate_renderProducts(area, date);

		for (int i = 0; i < renderProducts.size(); i++) {

			if (renderProducts.get(i).name.equalsIgnoreCase("mop18")) {
				mop18 += renderProducts.get(i).quantity;
				// System.out.println(renderProducts.size());
				// System.out.println(mop18);

				if (renderProducts.get(i).smell.equalsIgnoreCase("jasmine"))
					mop18Jasmine = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("straw"))
					mop18Straw = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("5o5"))
					mop185o5 = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("lemon"))
					mop18Lemon = renderProducts.get(i).quantity;

			}
			if (renderProducts.get(i).name.equalsIgnoreCase("mit")) {
				mit += renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("jasmine"))
					mitJasmine = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("straw"))
					mitStraw = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("5o5"))
					mit5o5 = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("lemon"))
					mitLemon = renderProducts.get(i).quantity;
			}

			if (renderProducts.get(i).name.equalsIgnoreCase("hd1")) {
				hd1 += renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("jasmine"))
					hd1Jasmine = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("straw"))
					hd1Straw = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("5o5"))
					hd15o5 = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("lemon"))
					hd1Lemon = renderProducts.get(i).quantity;
			}

			if (renderProducts.get(i).name.equalsIgnoreCase("hd2")) {
				hd2 += renderProducts.get(i).quantity;

				if (renderProducts.get(i).smell.equalsIgnoreCase("jasmine"))
					hd2Jasmine = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("straw"))
					hd2Straw = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("5o5"))
					hd25o5 = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("lemon"))
					hd2Lemon = renderProducts.get(i).quantity;

			}
			if (renderProducts.get(i).name.equalsIgnoreCase("mop36")) {
				mop36 += renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("jasmine"))
					mop36Jasmine = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("straw"))
					mop36Straw = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("5o5"))
					mop365o5 = renderProducts.get(i).quantity;
				if (renderProducts.get(i).smell.equalsIgnoreCase("lemon"))
					mop36Lemon = renderProducts.get(i).quantity;

			}

		}
		render(mop18, mop18Jasmine, mop18Straw, mop185o5, mop18Lemon, mit,
				mitJasmine, mitStraw, mit5o5, mitLemon, hd1, hd15o5,
				hd1Jasmine, hd1Lemon, hd1Straw, hd2, hd25o5, hd2Jasmine,
				hd2Lemon, hd2Straw, mop36, mop365o5, mop36Jasmine, mop36Lemon,
				mop36Straw);

	}

	// ////////////////////////////////////////////////////////////////////////////////////
	public static void jsonProducts(String name, String id) {

		ArrayList<Product> allProducts = new ArrayList<Product>();
		renderJSON(allProducts);

	}

	public static void Update(String users) {
		String json = users.replace('[', ' ').replace(']', ' ');
		String[] emparr = json.trim().split("},");
		for (int i = 0; i < emparr.length; i++) {
			String cleamemp = "";
			if (i == (emparr.length - 1)) {

				cleamemp = emparr[i].trim();
			} else {
				cleamemp = emparr[i].trim() + "}";

			}
			Gson gson = new Gson();
			Type type = new TypeToken<HashMap<String, String>>() {
			}.getType();
			HashMap<String, String> map = new HashMap<String, String>();
			map = gson.fromJson(cleamemp.trim(), type);
			User updated = User.findById(Long.parseLong(map.get("id"), 10));
			updated.username = map.get("username");
			updated.password = map.get("password");
			updated.save();

		}
	}

	public static void CreateProduct(String products) {

		String json = products.replace('[', ' ').replace(']', ' ');
		String[] emparr = json.trim().split("},");

		for (int i = 0; i < emparr.length; i++) {
			String cleamemp = "";
			if (i == (emparr.length - 1)) {

				cleamemp = emparr[i].trim();
			} else {
				cleamemp = emparr[i].trim() + "}";

			}
			Gson gson = new Gson();
			Type type = new TypeToken<HashMap<String, String>>() {
			}.getType();
			HashMap<String, String> map = new HashMap<String, String>();
			map = gson.fromJson(cleamemp.trim(), type);

			Product product = new Product(map.get("name"), Integer.parseInt(map
					.get("quantity")), Integer.parseInt(map
					.get("number_of_changes")), Double.parseDouble(map
					.get("price")), map.get("smell"), new LinkedList(),
					map.get("floor_type"));

			// product.addChangeTimes(product,Integer.parseInt(map.get("number_of_changes")),
			// Date);
			// product.save();
			// System.out.println(product.toString());

		}

	}

	public static void Delete(String users) {
		String json = users.replace('[', ' ').replace(']', ' ');
		String[] emparr = json.trim().split("},");

		for (int i = 0; i < emparr.length; i++) {
			String cleamemp = "";
			if (i == (emparr.length - 1)) {

				cleamemp = emparr[i].trim();
			} else {
				cleamemp = emparr[i].trim() + "}";

			}
			Gson gson = new Gson();
			Type type = new TypeToken<HashMap<String, String>>() {
			}.getType();
			HashMap<String, String> map = new HashMap<String, String>();
			map = gson.fromJson(cleamemp.trim(), type);
			User deleted = User.findById(Long.parseLong(map.get("id"), 10));
			deleted.delete();

		}

	}

}